package pers.cj.pdos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import pers.cj.pdos.entity.Goods;
import pers.cj.pdos.entity.GoodsStock;
import pers.cj.pdos.mapper.GoodsStockMapper;
import pers.cj.pdos.service.IGoodsStockService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import pers.cj.pdos.utils.Utils;
import pers.cj.pdos.vo.CountVo;
import pers.cj.pdos.vo.GoodsVo;
import pers.cj.pdos.vo.HouseQueryDetailVo;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Service
public class GoodsStockServiceImpl extends ServiceImpl<GoodsStockMapper, GoodsStock> implements IGoodsStockService {
    @Autowired
    GoodsStockMapper goodsStockMapper;

    @Override
    public GoodsStock getByGoodsAndHouse(String goodsId, String houseId) {
        return goodsStockMapper.selectOne(new QueryWrapper<GoodsStock>().eq("goods_id",goodsId).eq("house_id",houseId));
    }

    @Override
    public long countByGoods(String goodsId) {
        return goodsStockMapper.selectCount(new QueryWrapper<GoodsStock>().eq("goods_id",goodsId));
    }

    @Override
    public List<GoodsStock> listByGoods(String goodsId) {
        return goodsStockMapper.selectList(new QueryWrapper<GoodsStock>().eq("goods_id",goodsId));
    }

    @Override
    public List<GoodsStock> listByHouseAndGoodsList(String houseId, List list) {
        return goodsStockMapper.listByHouseAndGoodsList(houseId,list);
    }

    @Override
    public List<Map> listByHouseAndGoodsMap(String houseId, Map map) {
        return goodsStockMapper.listByHouseAndGoodsMap(houseId,map);
    }

    @Override
    public Map<String, Long> checkByGoodsList(List<Goods> goodsList) {
        List<CountVo> countVoList = goodsStockMapper.checkByGoodsList(goodsList);
        Map<String,Long> map = new HashMap<>();
        for (CountVo countVo : countVoList) {
            if (!countVo.getId().equals("0") ) {
                Long count = map.get(countVo.getId())==null ? Long.valueOf(0) : map.get(countVo.getId());
                count += countVo.getCount();
                map.put(countVo.getId(), count);
            }
        }
        return map;
    }

    @Override
    public boolean checkByGoods(String goodsId) {
        return goodsStockMapper.selectCount(new QueryWrapper<GoodsStock>().eq("goods_id",goodsId)) > 0;
    }

    @Override
    public List<GoodsVo> listVo(String companyId, Integer page, Integer limit, String goodsTypeId, Integer goodsStatus, Boolean filterZero, String search,String storeId,String houseId) {
        return goodsStockMapper.listVo(companyId, Utils.genPage(page,limit), limit, goodsTypeId, goodsStatus, filterZero, search,storeId,houseId);
    }

    @Override
    public long count(String companyId, String goodsTypeId, Integer goodsStatus, Boolean filterZero, String search,String storeId,String houseId) {
        return goodsStockMapper.count(companyId, goodsTypeId, goodsStatus, filterZero, search,storeId,houseId);
    }

    @Override
    public List<GoodsVo> listVoByGoodsIds(List<String> goodsIds, String storeId, String houseId) {
        return goodsStockMapper.listVoByGoodsIds(goodsIds, storeId, houseId);
    }

    @Override
    public BigDecimal getTotalStockAmount(String companyId, String storeId, String houseId) {
        return goodsStockMapper.getTotalStockAmount(companyId, storeId, houseId);
    }

    @Override
    public Map getHighestStockAmountWithGoods(String companyId, String storeId, String houseId) {
        return goodsStockMapper.getHighestStockAmountWithGoods(companyId, storeId, houseId);
    }

    @Override
    public List<Map> getAmountOrderByGoods(String companyId, String storeId, String houseId) {
        return goodsStockMapper.getAmountOrderByGoods(companyId, storeId, houseId);
    }

    @Override
    public List<Map> getAmountOrderByStore(String companyId, String storeId, String houseId) {
        return goodsStockMapper.getAmountOrderByStore(companyId, storeId, houseId);
    }

    @Override
    public List<Map> getAmountOrderByHouse(String companyId, String storeId, String houseId) {
        return goodsStockMapper.getAmountOrderByHouse(companyId, storeId, houseId);
    }

    @Override
    public List<Map> getNumberOrderByHouse(String companyId, String storeId, String houseId) {
        return goodsStockMapper.getNumberOrderByHouse(companyId, storeId, houseId);
    }

    @Override
    public List<HouseQueryDetailVo> listDetailListByGoods(Integer page,
                                                          Integer limit,
                                                          String goodsId,
                                                          String search,
                                                          String storeId,
                                                          String houseId) {
        return goodsStockMapper.listDetailListByGoods(Utils.genPage(page,limit), limit, goodsId,search,storeId,houseId);
    }

    @Override
    public Long countDetailListByGoods(String goodsId,
                                       String search,
                                       String storeId,
                                       String houseId) {
        return goodsStockMapper.countDetailListByGoods(goodsId, search,storeId,houseId);
    }

    @Override
    public List<Map> getGoodsStockGroupByHouse(String companyId, String goodsId, String storeId, String houseId) {
        return goodsStockMapper.getGoodsStockGroupByHouse(companyId, goodsId, storeId,houseId);
    }
}
